//
//  MXSStatusVM.swift
//  weibo
//
//  Created by 孙飞 on 15/12/11.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit

class MXSStatusVM: NSObject {
    
    var status: MXSStatus?{
        didSet{
            
            
        }
    }
    
    var vipImage: UIImage?{
//        if let mbrank = status?.user?.mbrank {
//            
//        }
        guard let mbrank = status?.user?.mbrank else{
            return nil
        }
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return nil
        
    }
    
    //认证类型 -1：没有认证; 1:认证用户，2,3,5: 企业认证，220: 达人
    var verifiedImage: UIImage? {
        guard let verified = status?.user?.verified else {
            return nil
        }
        
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return nil
        }
    }
    
    // 转发微博
    var retweetText: String?
    
    
    
    //  时间/来源
    var sourceText: String?
    var createAtText: String {
        
        guard let createDate = NSDate.sinaDate(status!.created_at!) else {
            return ""
        }
        return createDate.sinaDateString
    }
    
    
    
    var retweetStr: String = "转发"
    var commentStr: String = "评论"
    var attitudeStr: String = "赞"
    
    init(status: MXSStatus){
        super.init()
        self.status = status
        
        retweetStr = getCountString(self.status?.reposts_count ?? 0, defaulTitle: "转发")
        commentStr = getCountString(self.status?.comments_count ?? 0, defaulTitle: "评论")
        attitudeStr = getCountString(self.status?.attitudes_count ?? 0, defaulTitle: "赞")
        
        // 计算转发微博内容的逻辑
        if let retweetStatus = status.retweeted_status {
            if let name = retweetStatus.user?.name {
//                retweetText =  @转发微博的那一条微博用户的昵称: + 转发微博的内容
                retweetText = "@\(name):\(retweetStatus.text!)"
            }
        }
        
        // 来源字符串
        // <a href="http://weibo.com" rel="nofollow">新浪微博</a>
        
        //先判断source
        if let source = status.source where source.containsString("\">") {
            // 判断是否能够找到range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            sourceText = "来自 \(source.substringWithRange(startRange.endIndex..<endRange.startIndex))"
        }
        
        
    }
    
    
    private func getCountString(count: Int, defaulTitle: String) -> String {
        
        if count > 0 {
            // 小于1万显示原数据
            if count < 10000 {
                return "\(count)"
            }else {
                let result = CGFloat(count / 1000) / 10
                
                let resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            }
        }
        return defaulTitle
    }
    
    
    // ***** 
    override var description: String {
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }

}
